<template>

  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="模糊查询"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="selectDept"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="部门状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.id"
            :label="dict.format"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="selectDept">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" icon="el-icon-plus"  size="mini" @click="handleAdd">添加</el-button>
      </el-form-item>

    </el-form>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="deptList"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="name" label="部门名称" width="260"></el-table-column>
      <el-table-column align="center" label="状态" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @change="statusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
          >新增
          </el-button>
          <el-button
            v-if="scope.row.parentId != 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>


    <!-- 添加或修改部门对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="上级id" prop="parentId">
              <el-input v-model="form.parentId" placeholder="请输入部门id" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.parentId !== 0">
            <el-form-item label="上级部门" prop="name">
              <el-input v-model="form.parentName" placeholder="请输入部门名称" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="部门名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入部门名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="显示排序" prop="sortCode">
              <el-input-number v-model="form.sortCode" controls-position="right" :min="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="负责人" prop="leader">
              <el-input v-model="form.leader" placeholder="请输入负责人" maxlength="20"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="角色状态">
              <el-select v-model="form.status" placeholder="请选择">
                <el-option
                  v-for="dict in statusOptions"
                  :key="dict.id"
                  :label="dict.format"
                  :value="dict.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: "user",
  // components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      refreshTable: true,
      // 表格数据
      deptList: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      sum: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态数据
      statusOptions: [
        {
          "id": "0",
          "format": "正常"
        },
        {
          "id": "1",
          "format": "禁用"
        }
      ],
      sexOptions: [],
      // 日期范围
      dateRange: [],
      form: {},
      // 查询参数
      queryParams: {
      "createBy": "",
      "createTime": "",
      "id": "",
      "isDeleted": "",
      "leader": "",
      "name": "",
      "parentId": "",
      "sortCode": "",
      "status": "",
      "updateBy": "",
      "updateTime": ""
      },
      // 列信息
      columns: [],
      // 表单校验
      rules: {}
    };
  },
  watch: {},
  created() {
    this.selectDept();
    // this.getRequest("/common/getStatus").then(data => {
    //   this.statusOptions = data.data;
    // });
  },
  methods: {
    //查询
    selectDept() {
      this.postRequest("/sys-dept/cx", this.queryParams).then(data => {
        this.deptList = data.data;
        // this.sum = data.data.total;
        this.loading = false;
      }).catch(() => {
        this.deptList = null;
        this.loading = false;
      })
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      console.log(row);
      if (row != undefined) {
        // this.form.parentId = row.deptId;
        // this.form.parentName = row.deptId + '-' + row.name;
        this.form.parentName = row.name;
        this.form.parentId = row.id;


      }
      this.open = true;
      this.title = "添加部门";
      this.selectDept();
    },
    // 用户状态修改
    statusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      alert(row.status)
      this.$confirm('确认要"' + text + '""' + row.name + '"用户吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.postRequest("/sys-dept/zt", {
          "id": row.id,
          "status": row.status
        })
      }).catch(() => {
        row.status = row.status === "0" ? "1" : "0";
      });

    },
    /** 重置按钮操作 */
    resetQuery() {
      if (this.$refs["queryForm"]) {
        this.$refs["queryForm"].resetFields();
      }
      this.selectDept();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除部门编号为"' + row.id + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.postRequest("/sys-dept/Delete", {
          "id": row.id
        }).then(data => {
          this.selectDept();
          this.$message({
            type: 'success',
            message: '删除成功'
          });
        })
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.postRequest("/sys-dept/cx", {
        "id": row.id,
        "createBy": "",
        "createTime": "",
        "isDeleted": "",
        "leader": "",
        "name": "",
        "parentId": "",
        "sortCode": "",
        "status": "",
        "updateBy": "",
        "updateTime": ""
      }).then(data => {
        console.log(data.data);
        this.form.id = data.data[0].id;
        this.form.parentId = data.data[0].parentId;
        this.form.parentName = data.data[0].name;
        this.form.name = data.data[0].name;
        this.form.leader = data.data[0].leader;
        this.form.sortCode = data.data[0].sortCode;
        this.form.status = data.data[0].status;
        this.open = true;
      })
    }, // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        status: undefined,
      };
      if (this.$refs["form"]) {
        this.$refs["form"].resetFields();
      }
    },
    clickAdd() {
      this.open = true;
      this.reset();
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            this.postRequest("/sys-dept/ModifyThe", this.form).then(data => {
              this.open = false;
              this.selectDept();
              this.reset();
              this.$message({
                type: 'success',
                message: '修改成功'
              });
            })
          } else {
            this.postRequest("/sys-dept/Addition", this.form).then(data => {
              this.open = false;
              this.selectDept();
              this.reset();
              this.$message({
                type: 'success',
                message: '添加成功'
              });
            })
          }
        }
      });
    }, // 取消按钮
    cancel() {
      this.open = false;
    }
  }
};
</script>
